package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.ProfitMoneyMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProfitMoney;
import com.ruoyi.system.service.IProfitMoneyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 财务明细Controller
 * 
 * @author ruoyi
 * @date 2022-05-24
 */
@RestController
@RequestMapping("/system/money")
public class ProfitMoneyController extends BaseController
{
    @Autowired
    private IProfitMoneyService profitMoneyService;

    /**
     * 查询财务明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:money:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProfitMoney profitMoney)
    {
        startPage();
        List<ProfitMoney> list = profitMoneyService.selectProfitMoneyList(profitMoney);
        return getDataTable(list);
    }

    /**
     * 导出财务明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:money:export')")
    @Log(title = "财务明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProfitMoney profitMoney)
    {
        List<ProfitMoney> list = profitMoneyService.selectProfitMoneyList(profitMoney);
        ExcelUtil<ProfitMoney> util = new ExcelUtil<ProfitMoney>(ProfitMoney.class);
        util.exportExcel(response, list, "财务明细数据");
    }

    /**
     * 获取财务明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:money:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(profitMoneyService.selectProfitMoneyById(id));
    }

    @Resource
    private ProfitMoneyMapper profitMoneyMapper;

    @PreAuthorize("@ss.hasPermi('system:money:userToday')")
    @GetMapping("/userToday")
    public AjaxResult userToday(@RequestParam("userId")String userId)
    {
        Date date =new Date();
        DateTime beginOfDay = DateUtil.beginOfDay(date);
        DateTime endOfDay =DateUtil.endOfDay(date);


        List<ProfitMoney> profitMonies = profitMoneyMapper.selectList(userId,beginOfDay,endOfDay);
        BigDecimal total =new BigDecimal(0);
        for (ProfitMoney profitMony : profitMonies) {

            total=total.add(profitMony.getChange());

        }
        logger.info("total:{}",total);

        return AjaxResult.success(total);
    }

    public static long getTodayStartTime() {
        //设置时区
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }


    /**
     * 新增财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:money:add')")
    @Log(title = "财务明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProfitMoney profitMoney)
    {
        return toAjax(profitMoneyService.insertProfitMoney(profitMoney));
    }

    /**
     * 修改财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:money:edit')")
    @Log(title = "财务明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProfitMoney profitMoney)
    {
        return toAjax(profitMoneyService.updateProfitMoney(profitMoney));
    }

    /**
     * 删除财务明细
     */
    @PreAuthorize("@ss.hasPermi('system:money:remove')")
    @Log(title = "财务明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(profitMoneyService.deleteProfitMoneyByIds(ids));
    }
}
